package main.java.com.leng.SolorJ;

import com.leng.pojo.ProductModel;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by presoul on 2017/6/17.
 */
@Repository
public class JdDaoImpl implements JdDao {

    @Autowired
    private SolrClient client;

    public List<ProductModel> selectProductModelListByQuery(String queryString,
                                                            String catalog_name,
                                                            String price,
                                                            String sort) {


        SolrQuery params = new SolrQuery();


        params.setQuery(queryString);

        if (null != catalog_name && !"".equals(catalog_name)) {
            params.set("fq", "product_catalog_name:" + catalog_name + "");
        }

        if (null != price && !"".equals(price)) {

            String[] p = price.split("-");

            params.set("fq", "product_price:["+p[0]+" TO "+p[1]+"");
        }

        if ("1".equals(sort)){
            params.addSort("product_price", SolrQuery.ORDER.desc);
        }else {
            params.addSort("product_price", SolrQuery.ORDER.asc);
        }


        params.setStart(0);
        params.setRows(16);


        params.set("df", "product_keywords");
        params.set("f1", "id,product_name,product_price,product_catalog_name");

        params.setHighlight(true);
        params.addHighlightField("product_name");
        params.setHighlightSimplePre("<span style='color:red'>");
        params.setHighlightSimplePost("<span/>");


        QueryResponse response = null;

        try {
            response = client.query(params);
        } catch (SolrServerException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        SolrDocumentList docs = response.getResults();


        Map<String, Map<String, List<String>>> highlighting = response.getHighlighting();


       // long numFound = docs.getNumFound();

        //System.out.println("总条数：" + numFound + "\n");
        List<ProductModel> productModels = new ArrayList<>();

        for (SolrDocument docc : docs) {

            ProductModel productModel = new ProductModel();

            productModel.setPid((String) docc.get("id"));
            productModel.setName((String) docc.get("product_name"));
            productModel.setPrice((Float) docc.get("product_price"));
            productModel.setPicture((String) docc.get("product_picture"));

            productModels.add(productModel);
        }

        return productModels;
    }
}


